package com.edu.chongdian;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.tbruyelle.rxpermissions3.RxPermissions;

import io.reactivex.rxjava3.functions.Consumer;

public class MapActivity extends AppCompatActivity {
    MapView mapView;
    AMap aMap;
    String address,xiangXiAddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        new RxPermissions(this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE
                        ,Manifest.permission.READ_EXTERNAL_STORAGE
                        ,Manifest.permission.ACCESS_WIFI_STATE
                        ,Manifest.permission.READ_PHONE_STATE
                        ,Manifest.permission.ACCESS_COARSE_LOCATION
                        ,Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Throwable {
                        if (aBoolean) {
                            AMapLocationClient.updatePrivacyShow(MapActivity.this, true, true);
                            AMapLocationClient.updatePrivacyAgree(MapActivity.this, true);
                            initView(savedInstanceState);
                        }
                    }
                });
    }
    void initView(Bundle savedInstanceState){
        //map控件绑定
        mapView =findViewById(R.id.map_view);
        //创建地图
        mapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        //定位蓝点样式
        MyLocationStyle myLocationStyle=new MyLocationStyle();
        //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.interval(20000);
        myLocationStyle.showMyLocation(true);
        //去掉缩放倍数小时产生的紫色圆圈
        //设置边框颜色
        myLocationStyle.strokeColor(Color.TRANSPARENT)
                //设置圆形填充颜色
            .radiusFillColor(Color.argb(0,0,0,0))
                //设置边框粗细
            .strokeWidth(1.0f);
        //定位一次，且将视角移动到地图中心点。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE) ;
        //给地图设置定位蓝点
        aMap.setMyLocationStyle(myLocationStyle);
        //设置默认定位按钮是否显示，非必需设置。
        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        // 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        aMap.setMyLocationEnabled(true);
        //设置地图刚加载时的倍数
        CameraUpdate cameraUpdate= CameraUpdateFactory.zoomBy(10);
        aMap.moveCamera(cameraUpdate);
        // 设置Marker的经纬度坐标
        LatLng latLng = new LatLng(34.235206,108.893176);
        // 创建MarkerOptions对象
        MarkerOptions markerOptions = new MarkerOptions()
                .position(latLng);
                //.icon(BitmapDescriptorFactory.fromResource(R.mipmap.usericon05));
        // 在地图上添加Marker，并可点击
        aMap.addMarker(markerOptions).setClickable(true);
        // 设置Marker点击事件
        aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                MarkerDialog.Builder builder=new MarkerDialog.Builder(MapActivity.this);
                builder.setAddress(address);
                builder.setXiangXiAddress(xiangXiAddress);
                builder.setYueKaOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                builder.setYuEOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                builder.setDaoHangOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MapUtil.openGaodeNavigation(MapActivity.this,address,latLng.latitude,latLng.longitude);
                    }
                });
                MarkerDialog dialog=builder.create();
                dialog.show();
              return true;
            }
        });
        try {
            //初始化定位
            AMapLocationClient client=new AMapLocationClient(this);
            //配置定位参数
            AMapLocationClientOption option=new AMapLocationClientOption();
            //选择高精度定位
            option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //是否连续定位
            option.setOnceLocationLatest(true);
            //开启辅助wifi定位
            option.setWifiScan(true);
            client.setLocationOption(option);
            ProgressDialog dialog=new ProgressDialog(this);
            dialog.setMessage("定位中,请稍后");
            dialog.setCancelable(false);
            //设置监听回调
            client.setLocationListener(new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {
                    if (null!=aMapLocation){
                        if (aMapLocation.getErrorCode()==0){
                            dialog.dismiss();
                            Log.d("ADDRESS","定位地点"+aMapLocation.getProvince()+" "+aMapLocation.getCity()+" "+
                                    aMapLocation.getDistrict()+" "+aMapLocation.getStreet());
                            address=aMapLocation.getStreet();
                            xiangXiAddress=aMapLocation.getProvince()+" "+aMapLocation.getCity()+" "+
                                    aMapLocation.getDistrict()+" "+aMapLocation.getStreet();
                        }

                    }
                }

            });

            client.startLocation();
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        if (mapView!=null){
            mapView.onDestroy();
        }

    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }
}